package org.spring.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring容器中的Bean几种初始化方法和销毁方法的先后顺序 <br>
 * 
 * Bean在实例化的过程中：Constructor > @PostConstruct > InitializingBean > init-method
 * 
 * Bean在销毁的过程中：@PreDestroy > DisposableBean > destroy-method
 *
 * @author liubin
 *
 * @date 2018年3月1日上午10:17:03
 */
public class InitAndDestroySeqBean implements InitializingBean, DisposableBean {

    public InitAndDestroySeqBean() {
        System.out.println("执行InitAndDestroySeqBean: 构造方法");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("执行InitAndDestroySeqBean: postConstruct");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");
    }

    public void initMethod() {
        System.out.println("执行InitAndDestroySeqBean: init-method");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("执行InitAndDestroySeqBean: preDestroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("执行InitAndDestroySeqBean: destroy");
    }

    public void destroyMethod() {
        System.out.println("执行InitAndDestroySeqBean: destroy-method");
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        context.close();
    }
}
